import com.cy.dao.IUserDao;
import com.cy.io.Resources;
import com.cy.pojo.User;
import com.cy.sqlSession.SqlSession;
import com.cy.sqlSession.SqlSessionFactory;
import com.cy.sqlSession.SqlSessionFactoryBuild;
import org.dom4j.DocumentException;
import org.junit.Before;
import org.junit.Test;

import java.beans.PropertyVetoException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.List;

public class IPersistence_test {

    private IUserDao userDao;

    @Before
    public void before() throws PropertyVetoException, DocumentException, ClassNotFoundException {
        InputStream in = Resources.getResourcesAsInputStream("sqlMapConfig.xml");

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuild().build(in);

        SqlSession sqlSession = sqlSessionFactory.openSession();

        userDao = sqlSession.getMapper(IUserDao.class);
    }

    @Test
    public void testSelect(){
        try {

//            User user = new User();
//            user.setId(2);
//
//            User resultUser = sqlSession.selectOne("User.selectOne", user);

//            System.out.println(resultUser);

//            List<User> result = sqlSession.selectList("User.selectAll");
//
//            for (User user : result) {
//                System.out.println(user);
//            }
            List<User> result = userDao.findAll();

            for (User user : result) {
                System.out.println(user);
            }

            System.out.println("------------------");

            User user = new User();
            user.setId(1);
            User resultUser = userDao.findOne(user);
            System.out.println(resultUser);
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }

    @Test
    public void testInsert() throws PropertyVetoException, DocumentException, ClassNotFoundException, SQLException, NoSuchFieldException, IllegalAccessException {

        User user = new User();
        user.setUsername("testInsert");
        user.setPassword("testPassword");

        //普通调用方法
//        int count = sqlSession.insert("com.cy.dao.IUserDao.insert", user);
        //反射调用方法
        int count = userDao.insert(user);

        System.out.println("添加用户成功数：" + count);

    }

    @Test
    public void testUpdate() throws PropertyVetoException, DocumentException, ClassNotFoundException, SQLException, NoSuchFieldException, IllegalAccessException {

        User user = new User();
        user.setId(8);
        user.setUsername("testInsertUpdate");
        user.setPassword("testPasswordUpdate");

        //普通调用方法
//        int count = sqlSession.insert("com.cy.dao.IUserDao.update", user);

        //反射调用方法
        int count = userDao.updateById(user);

        System.out.println("更新用户成功数：" + count);

    }

    @Test
    public void testDelete() throws PropertyVetoException, DocumentException, ClassNotFoundException, SQLException, NoSuchFieldException, IllegalAccessException {

        User user = new User();
        user.setId(8);

        //普通调用方法
//        int count = sqlSession.delete("com.cy.dao.IUserDao.deleteById", user);
        //反射调用方法
        int count = userDao.deleteById(user);

        System.out.println("删除用户成功数：" + count);

    }
}
